package com.hjjt.filelib

import java.io.File
import java.io.FileInputStream
import java.math.BigInteger
import java.security.MessageDigest

/**
 *
 * @ProjectName:    PluginDemo
 * @Package:        com.hjjt.filelib
 * @ClassName:      MD5Util
 * @Description:
 * @Author:         孙浩
 * @CreateDate:     2022/6/13 11:39
 */
object MD5Util {
    fun checkMD5(filePath:String):String?{
        val file = File(filePath)
        if (!file.isFile()) {
            return null
        }
        var digest: MessageDigest? = null
        var `in`: FileInputStream? = null
        val buffer = ByteArray(1024)
        var len: Int
        try {
            digest = MessageDigest.getInstance("MD5")
            `in` = FileInputStream(file)
            while (`in`.read(buffer, 0, 1024).also { len = it } != -1) {
                digest.update(buffer, 0, len)
            }
            `in`.close()
        } catch (e: Exception) {
            e.printStackTrace()
            return null
        }
        val bigInt = BigInteger(1, digest.digest())
        return bigInt.toString(16)
    }
}